import RPi.GPIO as GPIO
import time

delay=2 #延时 2ms

pin_2 = 2
pin_3  = 3
pin_17 = 17
pin_27  = 27
 
GPIO.setmode(GPIO.BCM) #设置引脚的编码方式
   
def init():
    GPIO.setwarnings(False)
    GPIO.setup(pin_2, GPIO.OUT)
    GPIO.setup(pin_3, GPIO.OUT)
    GPIO.setup(pin_17, GPIO.OUT)
    GPIO.setup(pin_27, GPIO.OUT)
 
def setStep(w1, w2, w3, w4):
  GPIO.output(pin_2, w1)
  GPIO.output(pin_3, w2)
  GPIO.output(pin_17, w3)
  GPIO.output(pin_27, w4)

def forward(delay):  
    setStep(1, 0, 0, 0)
    time.sleep(delay)
    setStep(0, 1, 0, 0)
    time.sleep(delay)
    setStep(0, 0, 1, 0)
    time.sleep(delay)
    setStep(0, 0, 0, 1)
    time.sleep(delay)
  
def backward(delay): 
    setStep(0, 0, 0, 1)
    time.sleep(delay)
    setStep(0, 0, 1, 0)
    time.sleep(delay)
    setStep(0, 1, 0, 0)
    time.sleep(delay)
    setStep(1, 0, 0, 0)
    time.sleep(delay) 

if __name__ == "__main__":
    p = int(input()) # 1-360度  2-180度  3-135度  4-45度
    init() 
    while True:
        if p != 0:
            if p == 1:
                for i in range(0,512,1): #正转360度，延时1秒，反转360度
                    forward(int(delay) / 1000.0)
                time.sleep(2)
                for i in range(0,512,1):
                    backward(int(delay) / 1000.0)
            
            if p == 2:
                for i in range(0,256,1): #正转180度，延时1秒，反转180度
                    forward(int(delay) / 1000.0)
                time.sleep(1)
                for i in range(0,256,1):
                    backward(int(delay) / 1000.0)

            if p == 3:
                for i in range(0,192,1): #正转135度，延时1秒，反转135度
                    backward(int(delay) / 1000.0)
                time.sleep(1)
                for i in range(0,192,1):
                    forward(int(delay) / 1000.0)

            if p == 4:
                for i in range(0,64,1):  #正转45度，延时1秒，反转45度
                    backward(int(delay) / 1000.0)
                time.sleep(1)
                for i in range(0,64,1):
                    forward(int(delay) / 1000.0)
        break
    GPIO.cleanup()
